package cn.hznc.controller.device.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hznc.controller.service.device.DeviceService;
import cn.hznc.domain.entity.ProgramDurationTimeDailyInfoEntity;
import cn.hznc.domain.entity.ProgramDurationTimeEntity;
import cn.hznc.mapper.MachineToolStatusDailyInfoMapper;
import cn.hznc.mapper.MachineToolStatusMapper;
import cn.hznc.mapper.ProgramDurationTimeDailyInfoMapper;
import cn.hznc.mapper.ProgramDurationTimeMapper;
import cn.hznc.repository.ProgramDurationTimeRepository;
import cn.hznc.repository.WarehouseRespository;
import cn.hznc.utils.CommonUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author：Administrator
 * @Date：2024/5/9 10:54
 */
@Component
@Slf4j
public class MachineToolStatusTask{

    @Resource
    private MachineToolStatusDailyInfoMapper statusDailyInfoMapper;

    @Resource
    private MachineToolStatusMapper machineToolStatusMapper;

    @Resource
    private WarehouseRespository warehouseRespository;

    @Resource
    private DeviceService deviceService;

    @Resource
    private ProgramDurationTimeMapper programDurationTimeMapper;

    @Resource
    private ProgramDurationTimeRepository programDurationTimeRepository;

    @Resource
    private ProgramDurationTimeDailyInfoMapper programDurationTimeDailyInfoMapper;


    SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");

//    /**
//     * 将当天的机床状态记录存到每日机床状态统计表中
//     */
//    @Scheduled(cron ="59 59 23 * * ? ")
//    public void statisticsStatusRunningAndOpeningRate(){
//        //获取当天各个时间段的列表
//        List<GapHourDto> dayHourlyGap = CommonUtil.getDayHourlyGap();
//        Date currentDateTime = new Date();
//        String dayTime = sdfDate.format(currentDateTime);
//
//        List<MachineToolStatusDailyInfoEntity> insertList = new ArrayList<>();
//        List<WarehouseSnVo> warehouseSnVo = warehouseRespository.selectWarehouseSnVo();
//        List<String> machineToolNames = warehouseSnVo.stream().map(WarehouseSnVo::getMachineToolName).collect(Collectors.toList());
//        Map<String , String> nameSnMap = warehouseSnVo.stream().collect(Collectors.toMap(WarehouseSnVo::getMachineToolName , WarehouseSnVo::getSnCode));
//        if(CollectionUtil.isNotEmpty(dayHourlyGap)){
//            for (int i = 0; i < dayHourlyGap.size(); i++) {
//                GapHourDto gapHourDto = dayHourlyGap.get(i);
//                //获取该时段机床运行率
//                DeviceRunningReq deviceRunningReq = new DeviceRunningReq();
//                deviceRunningReq.setDeviceList(machineToolNames);
//                deviceRunningReq.setStartTime(gapHourDto.getStart());
//                deviceRunningReq.setEndTime(gapHourDto.getEnd());
//                List<DeviceRateVo> deviceRunningRateVos = deviceService.deviceRunning(deviceRunningReq , nameSnMap);
//
//                //获取该时段机床开机率
//                DeviceRunningReq deviceOpeningReq = new DeviceRunningReq();
//                deviceOpeningReq.setDeviceList(machineToolNames);
//                deviceOpeningReq.setStartTime(gapHourDto.getStart());
//                deviceOpeningReq.setEndTime(gapHourDto.getEnd());
//                List<DeviceRateVo> deviceOpeningRateVos = deviceService.deviceOpeningTask(deviceRunningReq , nameSnMap);
//                if(CollectionUtil.isNotEmpty(machineToolNames)){
//                    for (String machineToolName : machineToolNames) {
//                        MachineToolStatusDailyInfoEntity machineToolStatusDailyInfoEntity = new MachineToolStatusDailyInfoEntity();
//                        for (DeviceRateVo deviceRunningRateVo : deviceRunningRateVos) {
//                            if(deviceRunningRateVo.getName().equals(machineToolName)){
//                                machineToolStatusDailyInfoEntity.setRunningRate(deviceRunningRateVo.getRate().intValue());
//                            }
//                        }
//
//                        for (DeviceRateVo deviceOpeningRateVo : deviceOpeningRateVos) {
//                            if(deviceOpeningRateVo.getName().equals(machineToolName)){
//                                machineToolStatusDailyInfoEntity.setOpeningRate(deviceOpeningRateVo.getRate().intValue());
//                            }
//                        }
//                        machineToolStatusDailyInfoEntity.setMachineToolName(machineToolName);
//                        machineToolStatusDailyInfoEntity.setSnCode(nameSnMap.get(machineToolName));
//                        machineToolStatusDailyInfoEntity.setDayTime(dayTime);
//                        machineToolStatusDailyInfoEntity.setCreateTime(currentDateTime);
//                        machineToolStatusDailyInfoEntity.setHour(i+1);
//                        insertList.add(machineToolStatusDailyInfoEntity);
//                    }
//                }
//            }
//        }
//
//        if(CollectionUtil.isNotEmpty(insertList)){
//            if (insertList.size() > 2000){
//                List<List<MachineToolStatusDailyInfoEntity>> gapInsertList = CommonUtil.groupList(insertList);
//                gapInsertList.forEach(singleInsertList -> statusDailyInfoMapper.insertBatchSomeColumn(singleInsertList));
//            }else {
//                statusDailyInfoMapper.insertBatchSomeColumn(insertList);
//            }
//        }
//    }

    @Scheduled(cron ="00 59 23 * * ? ")
    public void statisticsProgramDurationDaily(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        List<ProgramDurationTimeDailyInfoEntity> resultList = programDurationTimeRepository.selectDurationAvgTimeDaily();
        if(CollectionUtil.isNotEmpty(resultList)){
            if (resultList.size() > 2000){
                List<List<ProgramDurationTimeDailyInfoEntity>> gapInsertList = CommonUtil.groupList(resultList);
                gapInsertList.forEach(singleInsertList -> programDurationTimeDailyInfoMapper.insertBatchSomeColumn(singleInsertList));
            }else {
                programDurationTimeDailyInfoMapper.insertBatchSomeColumn(resultList);
            }
            //删除15天前的数据
            calendar.set(Calendar.HOUR_OF_DAY, -15);
            QueryWrapper<ProgramDurationTimeEntity> deleteWrapper = new QueryWrapper<>();
            deleteWrapper.le("create_time" , calendar.getTime());
            programDurationTimeMapper.delete(deleteWrapper);
        }
    }

//    /**
//     * 删除冗余的状态记录
//     */
//    @Scheduled(cron ="0 0 0 1 * ?")
//    public void deleteRedundantStatusRecord(){
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(new Date());
//        calendar.add(Calendar.MONTH , -1);
//        Date lastMonthTime = calendar.getTime();
//        QueryWrapper<MachineToolStatus> wrapper = new QueryWrapper<>();
//        wrapper.le("create_time" , lastMonthTime);
//        machineToolStatusMapper.delete(wrapper);
//    }


}
